<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of baiduMapClient
 * 腾讯地图相关搜索
 * @author Administrator
 */
class SOSOMapClient {

    private $api_server_url;
    private $auth_params;

    public function __construct() {
        $this->api_server_url = "http://apis.map.qq.com/";
        $this->auth_params = array();
        $this->auth_params['key'] = "NCTBZ-XCWRJ-LJ6F7-KOX5P-GAHAO-VUF6L"; //腾讯地图请求Key
        $this->auth_params['output'] = "json";
    }

    protected function Geocoding_coordinate_address($location) {
        return $this->call("geocoder", array("location" => $location));
    }

    protected function Place_search($query, $location, $radius) {
        return $this->call("ws/place/v1/search", array("keyword" => $query, "boundary" => "nearby(".$location.",".$radius.")", "output" => "json"));
    }
    
    //HTTP请求
    protected function call($method, $params = array()) {
        $headers = array(
            "User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1",
            "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
            "Accept-Language: en-us,en;q=0.5",
        );
        $params = array_merge($this->auth_params, $params);
        $url = $this->api_server_url . "$method?" . http_build_query($params);
       
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
        $data = curl_exec($ch);
        curl_close($ch);
        $result = null;
        if (!empty($data)) {
           
            $result = json_decode($data);
        }
        return $result;
    }

    /**
     * 根据腾讯地图请求附近的信息
     * @param type $entity 检索关键词
     * @param type $x 经度
     * @param type $y 维度
     * @param type $radius 检索半径
     * @return string 检索结果
     */
    function catchEntitiesFromLocation($entity, $x, $y, $radius) {
        //$mapObj = new baiduMapClient();
        $search = $this->Place_search($entity, $x . "," . $y, $radius);
        $results = $search->data;
        for ($i = 0; $i < count($results); $i++) {
            $distance =$results[$i]->_distance;
            $url="http://apis.map.qq.com/uri/v1/routeplan?type=drive&fromcoord=".$x.",".$y."&tocoord=".$results[$i]->location->lat.",".$results[$i]->location->lng."&policy=2&coord_type=2&referer=tengxun";
            $shopSortArrays[$distance] = array(
                
                "Title" => "【" . $results[$i]->title . "】<" . $distance . "M>" . $results[$i]->address . (isset($results[$i]->telephone) ? " " . $results[$i]->telephone : ""),
                "Description" => "",
                "PicUrl" => "",
                //http://map.qq.com/?type=bus&tactic=1&from=%E6%88%91%E7%9A%84%E5%AE%B6&fromcoord=39.980683%2C116.302&to=%E4%B8%AD%E5%85%B3%E6%9D%91&tocoord=39.9836%2C116.3164
                "Url"=>"http://apis.map.qq.com/uri/v1/routeplan?type=drive&fromcoord=".$x.",".$y."&tocoord=".$results[$i]->location->lat.",".$results[$i]->location->lng."&policy=2&coord_type=2&referer=usky"
                );
        }
        ksort($shopSortArrays); //排序
        $shopArray = array();
        foreach ($shopSortArrays as $key => $value) {
            $shopArray[] = array(
                "Title" => $value["Title"],
                "Description" => $value["Description"],
                "PicUrl" => $value["PicUrl"],
                "Url" => $value["Url"],
            );
            //筛选前6条(微信多图文限制)
            if (count($shopArray) > 6) {
                break;
            }
        }
        return $shopArray;
    }

   

}
